package dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.*;

import awesomeDocumentIO.AwesomeDocumentImporter;

/**
 * This dialog is used to create a new document type.
 * 
 * @author Brittin Source originally from:
 *         http://java.sun.com/docs/books/tutorial
 *         /uiswing/examples/components/DialogDemoProject
 *         /src/components/CustomDialog.java
 */
public class AddSyntaxColorDialog extends JDialog implements ActionListener,
		PropertyChangeListener {
	private String[] retVals;
	private JComboBox filetypes;
	private JTextField keywordTextField;
	private JOptionPane optionPane;
	private JComboBox colors;
	private String btnString1 = "Enter";
	private String btnString2 = "Cancel";

	/**
	 * Returns null if the typed strings were invalid; otherwise, returns the
	 * strings as the user entered them.
	 */
	public String[] getEnteredValues() {
		return retVals;
	}

	/** Creates the reusable dialog. */
	public AddSyntaxColorDialog(Frame aFrame) {
		super(aFrame, true);

		setTitle("Add New Syntax");
		filetypes = new JComboBox();
		HashMap<String, String> doctypes = AwesomeDocumentImporter
				.getAllDocInfo();
		for (String str : doctypes.keySet()) {
			filetypes.addItem(str);
		}
		keywordTextField = new JTextField(10);
		colors = new JComboBox();
		List<String> colorsList = getColors();
		for (String c : colorsList) {
			colors.addItem(c);
		}

		// Create an array of the text and components to be displayed.
		String msgString1 = "Please select the document type:";
		String msgString2 = "Please enter the keyword:";
		String msgString3 = "Please select a color:";
		Object[] array = { msgString1, filetypes, msgString2, keywordTextField,
				msgString3, colors };

		// Create an array specifying the number of dialog buttons
		// and their text.
		Object[] options = { btnString1, btnString2 };

		// Create the JOptionPane.
		optionPane = new JOptionPane(array, JOptionPane.PLAIN_MESSAGE,
				JOptionPane.YES_NO_OPTION, null, options, options[0]);

		// Make this dialog display it.
		setContentPane(optionPane);

		// Handle window closing correctly.
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				/*
				 * Instead of directly closing the window, we're going to change
				 * the JOptionPane's value property.
				 */
				optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
			}
		});

		// Ensure the text field always gets the first focus.
		addComponentListener(new ComponentAdapter() {
			public void componentShown(ComponentEvent ce) {
				filetypes.requestFocusInWindow();
			}
		});

		// Register an event handler that reacts to option pane state
		// changes.
		optionPane.addPropertyChangeListener(this);
		pack();
	}

	private List<String> getColors() {
		List<String> list = new ArrayList<String>();
		list.add("black");
		list.add("blue");
		list.add("cyan");
		list.add("dark_gray");
		list.add("gray");
		list.add("green");
		list.add("light_gray");
		list.add("magenta");
		list.add("orange");
		list.add("pink");
		list.add("red");
		list.add("white");
		list.add("yellow");
		return list;
	}

	/** This method handles events for the text field. */
	public void actionPerformed(ActionEvent e) {
		optionPane.setValue(btnString1);
	}

	/** This method reacts to state changes in the option pane. */
	public void propertyChange(PropertyChangeEvent e) {
		String prop = e.getPropertyName();

		if (isVisible()
				&& (e.getSource() == optionPane)
				&& (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY
						.equals(prop))) {
			Object value = optionPane.getValue();

			if (value == JOptionPane.UNINITIALIZED_VALUE) {
				// ignore reset
				return;
			}

			// Reset the JOptionPane's value.
			// If you don't do this, then if the user
			// presses the same button next time, no
			// property change event will be fired.
			optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);

			if (btnString1.equals(value)) {
				String documentLabel = (String) filetypes.getSelectedItem();
				String keywordText = keywordTextField.getText();
				String colorString = (String) colors.getSelectedItem();
				
				if (!"".equals(documentLabel) && !"".equals(keywordText)) {
					retVals = new String[3];
					retVals[0] = documentLabel;
					retVals[1] = keywordText;
					retVals[2] = colorString;
					clearAndHide();

				} else {
					// text was invalid
					keywordTextField.selectAll();
					JOptionPane.showMessageDialog(AddSyntaxColorDialog.this,
							"A keyword must be specified", "Please try again",
							JOptionPane.ERROR_MESSAGE);
					retVals = null;
					keywordTextField.requestFocusInWindow();
				}
			} else { // user closed dialog or clicked cancel
				retVals = null;
				clearAndHide();
			}
		}
	}

	/** This method clears the dialog and hides it. */
	public void clearAndHide() {
		keywordTextField.setText(null);
		setVisible(false);
	}
}
